<?php
namespace Admin\Controller;

/**
 * Class 店铺商品分类
 * @package Admin\Controller
 */
class StoreCatController extends BaseController {

    public function index(){

        $catModel = M('b2c_store_cat');
        $parent = $catModel->where(['level' => 1, 'local_id' => session("userInfo")['local_id']])->select();
        $second = $catModel->where(['level' => 2, 'local_id' => session("userInfo")['local_id']])->select();
        $three = $catModel->where(['level' => 3, 'local_id' => session("userInfo")['local_id']])->select();

        $this->assign(['parent' => $parent, 'second' => $second, 'three' => $three]);
        $this->display();
    }

    public function add(){
        $catModel = M('b2c_store_cat');
        $parent = $catModel->where(['level' => 1, 'local_id' => session("userInfo")['local_id']])->select();
        $second = $catModel->where(['level' => 2, 'local_id' => session("userInfo")['local_id']])->select();

        if ($_POST) {
            $parent_id = I('parent_id', '', intval());
            $cat_name = I('cat_name', '', htmlentities());
            $cat_img = I('cat_img', '', htmlentities());
            $cat_bg_img = I('cat_bg_img', '', htmlentities());
            $level_post = I('level', '', intval());

            $cat_img = $cat_img[0];
            $cat_bg_img = $cat_bg_img[0];

            if (empty($cat_name)) {
                $this->error('分类名不能找到');
            }

            // 查询父级分类级别
//            $cat_level = $catModel->where(['store_cat_id' => $parent_id, 'local_id' => session("userInfo")['local_id']])->field('level')->find();
//
//            $level = $cat_level['level'] + 1;

            $catModel = M('b2c_store_cat');

            $data = [
                'local_id' => session("userInfo")['local_id'],
                'parent_id' => $parent_id,
                'cat_name' => $cat_name,
                'cat_img' => $cat_img,
                'cat_bg_img' => $cat_bg_img,
                'level' => $level_post,
            ];
            $catModel->add($data);
            get_curl(session("userInfo")['local_id'], 'http://'.$_SERVER['SERVER_NAME'] . '/store/index/local_id/');
            $this->success("成功",'/admin/storeCat/index');exit;
        }

        $this->assign(['parent' => $parent, 'second' => $second]);
        $this->display();
    }

    public function edit(){
        $store_cat_id = I('id', '', intval());
        $catModel = M('b2c_store_cat');

        $parent = $catModel->where(['level' => 1, 'local_id' => session("userInfo")['local_id']])->select();
        $second = $catModel->where(['level' => 2, 'local_id' => session("userInfo")['local_id']])->select();

        $cat = $catModel->where(['store_cat_id' => $store_cat_id, 'local_id' => session("userInfo")['local_id']])->find();

        if ($_POST) {
            $parent_id = I('parent_id', '', intval());
            $cat_name = I('cat_name', '', htmlentities());
            $cat_img = I('cat_img', '', htmlentities());
            $cat_bg_img = I('cat_bg_img', '', htmlentities());
            $level_post = I('level', '', intval());

            if ($level_post == 2 && empty($parent_id)) {
                $this->error('请选择导航菜单');exit;
            }

            if (empty($cat_name)) {
                $this->error('分类名不能找到');
            }

            // 查询父级分类级别
//            $cat_level = $catModel->where(['store_cat_id' => $parent_id, 'local_id' => session("userInfo")['local_id']])->field('level')->find();
//
//            $level = $cat_level['level'] + 1;

            $catModel = M('b2c_store_cat');

            $data = [
                'local_id' => session("userInfo")['local_id'],
                'parent_id' => $parent_id,
                'cat_name' => $cat_name,
                'level' => $level_post,
            ];
            if (!empty($cat_img)) {
                $data['cat_img'] = $cat_img[0];
            }
            if (!empty($cat_bg_img)) {
                $data['cat_bg_img'] = $cat_bg_img[0];
            }

            $catModel->where(['store_cat_id' => $store_cat_id, 'local_id' => session("userInfo")['local_id']])->save($data);
            get_curl(session("userInfo")['local_id'], 'http://'.$_SERVER['SERVER_NAME'] . '/store/index/local_id/');
            $this->success("修改成功",'/admin/storeCat/index');exit;
        }

        $this->assign(['parent' => $parent, 'second' => $second, 'cat' => $cat]);
        $this->display();
    }

    /**
     * ajax 删除图片
     */
    public function deleteImg(){
        $id = I('id', '', intval());
        $type = I('type', '', intval());
        if (!empty($id) && !empty($type)) {
            $catModel = M('b2c_store_cat');
            $catModel->where(['local_id' => session('userInfo')['local_id'], 'store_cat_id' => $id])->save([$type => '']);
            echo 1;
        }
        get_curl(session("userInfo")['local_id'], 'http://'.$_SERVER['SERVER_NAME'] . '/store/index/local_id/');
    }

    public function delete(){
        $id = I('id', '', intval());
        $catModel = M('b2c_store_cat');
        $catMsg = $catModel->where(['local_id' => session('userInfo')['local_id'], 'store_cat_id' => $id])->find();
        if ($catMsg['level'] == 1 || $catMsg['level'] == 2) {
            $parent = $catModel->where(['local_id' => session('userInfo')['local_id'], 'parent_id' => $id])->find(); // 查询父级下面有分类就不擅长
            if ($parent) {
                $this->error('不能删除，下面有子分类');
                exit;
            }
        }
        $catModel->where(['local_id' => session('userInfo')['local_id'], 'store_cat_id' => $id])->delete();
        get_curl(session("userInfo")['local_id'], 'http://'.$_SERVER['SERVER_NAME'] . '/store/index/local_id/');
        $this->success('成功删除', '/admin/storeCat/index/');
    }
}